/*
 * responce.cpp
 *
 *  Created on: 2021年8月16日
 *      Author: pengzc
 */

#include "http_response.h"

#include "base/strings.hpp"

namespace http {

HTTPResponse::HTTPResponse() {
  // TODO 自动生成的构造函数存根
  status_code_ = 0;
//  error_code_ = 0;
  dst_file_ = NULL;
}

HTTPResponse::~HTTPResponse() {
  // TODO 自动生成的析构函数存根
}

int HTTPResponse::status() {
  return status_code_;
}

//int HTTPResponse::ErrorCode() {
//  return error_code_;
//}

std::string HTTPResponse::body() {
  return body_;
}

//std::string HTTPResponse::ErrorMessage() {
//  return error_message_;
//}

#include "utils/Log.h"
std::string HTTPResponse::header(const std::string& key) {
  return header_[key];
}

std::string* HTTPResponse::mutable_body() {
  return &body_;
}

int HTTPResponse::getContentLength() const {
  const char* key = "Content-Length";
  auto it = header_.find(key);
  if (it == header_.end()) {
    return -1;
  }
  return atoi(it->second.c_str());
}

std::string HTTPResponse::getContentType() const {
  const char* key = "Content-Type";
  auto it = header_.find(key);
  if (it == header_.end()) {
    return "";
  }
  return it->second;
}

std::string HTTPResponse::toString() {
  std::string str;
         str += "HTTP perform information\n";
         str += base::format("\t total_time: %lf\n", total_time);
         str += base::format("\t name_lookup_time: %lf\n", name_lookup_time);
         str += base::format("\t connect_time: %lf\n", connect_time);
         str += base::format("\t app_connect_time: %lf\n", app_connect_time);
         str += base::format("\t pre_transfer_time: %lf\n", pre_transfer_time);
         str += base::format("\t start_transfer_time: %lf\n", start_transfer_time);
         str += base::format("\t redirect_time: %lf\n", redirect_time);
         str += base::format("\t redirect_count: %d\n", redirect_count);
  return str;
}

const std::string& HTTPResponse::reasonText() const {
  return reason_text_;
}

} /* namespace http */
